package com.am.mosl.cityandroid.service;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import cn.jpush.android.api.JPushInterface;

import com.am.mosl.cityandroid.message.LoginMessage;
import com.am.mosl.cityandroid.message.MessageNumber;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import de.greenrobot.event.EventBus;

public class MessageReceiver extends BroadcastReceiver {
	
	private static final String TAG = "MyReceiver";
	private NotificationManager nm;
	SlidingMenu localSlidingMenu;

	@Override
	public void onReceive(Context context, Intent intent) {
		if (null == nm) {
			nm = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
		}

		Bundle bundle = intent.getExtras();
		Log.d(TAG, "onReceive - " + intent.getAction() + ", extras: ");

		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			Log.d(TAG, "JPush用户注册成功");

		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent
				.getAction())) {
			Log.d(TAG, "接受到推送下来的自定义消息");

		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent
				.getAction())) {
			Log.d(TAG, "接受到推送下来的通知");

			receivingNotification(context, bundle);

		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent
				.getAction())) {
			Log.d(TAG, "用户点击打开了通知");

			openNotification(context, bundle);

		} else {
			Log.d(TAG, "Unhandled intent - " + intent.getAction());
		}

	}

	private void receivingNotification(Context context, Bundle bundle) {
		String title = bundle
				.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
		Log.d(TAG, " title : " + title);
		String message = bundle.getString(JPushInterface.EXTRA_ALERT);
		Log.d(TAG, "message : " + message);
		String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
		Log.d(TAG, "extras : " + extras);

		LoginMessage message2 = new LoginMessage();
		message2.setType(2);
		EventBus.getDefault().post(message2);

		String myValue = "";
		JSONObject extrasJson;
		try {
			extrasJson = new JSONObject(extras);
			myValue = extrasJson.optString("badge");
			SetMessageNumner(Integer.parseInt(myValue));

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// 设置消息数量
	private void SetMessageNumner(int number) {
		MessageNumber message = new MessageNumber();
		message.setNumber(number);
		message.setStatus(MessageNumber.Status_Set);
		EventBus.getDefault().post(message);

	}

	// 取消消息数量
	private void CancelMessageNumber() {
		MessageNumber message = new MessageNumber();
		message.setStatus(MessageNumber.Status_Cancel);
		EventBus.getDefault().post(message);
	}

	private void openNotification(Context context, Bundle bundle) {
		String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
		String myValue = "";
		try {
			JSONObject extrasJson = new JSONObject(extras);
			myValue = extrasJson.optString("badge");
			Log.d("badge : ", myValue);

		} catch (Exception e) {
			Log.w(TAG, "Unexpected: extras is not a valid json", e);
			return;
		}

		// if (TYPE_THIS.equals(myValue)) {
		// Intent mIntent = new Intent(context, ThisActivity.class);
		// mIntent.putExtras(bundle);
		// mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// context.startActivity(mIntent);
		// } else if (TYPE_ANOTHER.equals(myValue)) {
		// Intent mIntent = new Intent(context, AnotherActivity.class);
		// mIntent.putExtras(bundle);
		// mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// context.startActivity(mIntent);
		// }
	}
}